//
//  BHCircleReviewPresentTextView.swift
//  BigHealth
//
//  Created by 蔺百好 on 2021/8/6.
//

import UIKit
protocol BHCircleReviewPresentTextViewDelegate {
    func textViewPublishReview(reviewText: String) // 发布
}

class BHCircleReviewPresentTextView: UIView {

    var delegate: BHCircleReviewPresentTextViewDelegate?
    
    
    @IBOutlet weak var text_view: UITextView!
    @IBOutlet weak var sendReviewBtn: UIButton!
    
    @IBOutlet weak var place_holder_label: UILabel!
    
    static func loadFromXib() -> BHCircleReviewPresentTextView {
        let viewArray = Bundle.main.loadNibNamed("BHCircleReviewPresentTextView", owner: nil, options: nil)
        let view: BHCircleReviewPresentTextView = viewArray![0] as! BHCircleReviewPresentTextView
        return view
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
        self.text_view.delegate = self
        
        self.place_holder_label.addViewAction {[weak self] (label) in
            self?.text_view.becomeFirstResponder()
        }
        
        self.text_view.addLayerCorner(5, 0, .white)
    }
    
    
    func becomeActive() {
        self.text_view.becomeFirstResponder()
    }
    func resignActive() {
        self.text_view.resignFirstResponder()
        self.place_holder_label.isHidden = false
        self.place_holder_label.text = String.reviewPlaceHolder
    }
    
    @IBAction func sendAct(_ sender: Any) {
        self.delegate?.textViewPublishReview(reviewText: self.text_view.text ?? "")
    }
    
    static var height: CGFloat {
        get {
            return 118
        }
    }
    
}
extension BHCircleReviewPresentTextView: UITextViewDelegate {
    
    func textViewDidChange(_ textView: UITextView) {
        
        if textView.text.isEmpty {
            self.place_holder_label.isHidden = false
            
            self.sendReviewBtn.isEnabled = false
        }else {
            self.place_holder_label.isHidden = true
            self.sendReviewBtn.isEnabled = true
        }
    }
    
    func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
        return true
    }
    
}
